<template>
	<view>
		<view class="goodslist-box">
			<view v-for="(goods,i) in goodsList" :key='i' @click="gotoDetail(goods)">
				<my-goods :goods="goods"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//商品列表数据
				goodsList: [],
				//总数量，用于分类
				total: 0,
				//参数列表
				queryObj: {
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 10
				}
			};
		},
		onLoad(options) {
			this.getGoodsList()
			//获取的参数
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
		},
		methods: {
			async getGoodsList(cb) {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)

				cb && cb()

				if (res.meta.status !== 200) return uni.$shouMsg()
				this.goodsList = [...this.goodsList, ...res.message.goods]
				this.total = res.message.total
			},
			gotoDetail(goods) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
				})
			}
		},
		onReachBottom() {
			if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$shouMsg('数据加载完毕！')
			//页码值+1
			this.queryObj.pagenum += 1
			//重新获取列表数据
			this.getGoodsList()
		},
		onPullDownRefresh() {
			this.total = 0,
				this.queryObj.pagenum = 1,
				this.goodsList = [],

				this.getGoodsList(() => uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss">

</style>